<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-01 16:40:00
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2022-03-26 22:39:22
 */

namespace app\admin\model;
use app\common\model\BaseNew;

class MerchantNode extends BaseNew {
	// 设置数据表（不含前缀）
	protected $name = '';

	// 定义时间戳字段名
	protected $createTime = '';
	protected $updateTime = '';

	// 定义排序字段
	protected $_orderField = 'vieworder';
	protected $_orderDesc = 'desc';

	/**
	 * 获取权限列表
	 */
	public function getListWithAccess($roleid) {
		//获取权限访问列表
		$accessDao = model('MerchantNodeAccess');
		list($accessList) = $accessDao->search([]);
		//echo '权限列表accessList:';print_r($accessList);exit;
		//获取权限列表
		$data = $this->order('vieworder desc')->select();
		$ruleList = $data->toArray(); //转换为数组
		//print_r($ruleList);
		$retRuleList = array();
		foreach ($ruleList as $key => $value) {
			$value['checked'] = $this->is_checked($value, $roleid, $accessList) ? ' checked' : '';
			$retRuleList[$value['id']] = $value;
		}
		return array($retRuleList);
	}

	/**
	 * 检查指定权限是否有访问
	 * @param array $data menu表中数组
	 * @param int $roleid 需要检查的角色ID
	 */
	protected function is_checked($data, $roleid, $accessArr) {
		if ($data['flag'] == '') {
			return false;
		}
		$mdata = array();
		$mdata['roleid'] = $roleid;
		$mdata["flag"] = $data['flag'];
		//print_r($mdata);
		//print_r($accessArr);exit;
		$check = in_array($mdata, $accessArr);
		if ($check) {
			return true;
		} else {
			return false;
		}
	}
	
	public function countSearch($map) {
		return $this->where($map)->count();
	}

	public function search($map, $limit='', $orderBy='') {
		if (!$orderBy) {
			$orderBy = [$this->_orderField=>$this->_orderDesc];
		}
		$data = $this->where($map)->order($orderBy)->limit($limit)->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	protected function parseSearch($data) {
		$ftpWeb = config('app.ftp_web');
		$isopen_config = config('engineeconfig.isopen_config');

		$list = array();
		foreach ($data as $key => $value) {
			//通用解析
			//$value['create_time_show'] = $value['create_time'] ? date("Y-m-d H:i:s", $value['create_time']) : '';
			//$value['state_show'] = isset($value['state']) ? $isopen_config[$value['state']] : '';
			//$value['picurl'] = $value['picurl'] ? $ftpWeb.$value['picurl'] : '';
			//通用解析

			$list[$value['id']] = $value;
		}
		return array($list);
	}

	protected function parseInfo($info) {
		$ftpWeb = config('app.ftp_web');
		$isopen_config = config('engineeconfig.isopen_config');

		//通用解析
		//$info['create_time_show'] = $info['create_time'] ? date("Y-m-d H:i:s", $info['create_time']) : '';
		//$info['state_show'] = isset($info['state']) ? $isopen_config[$info['state']] : '';
		//$info['picurl'] = $info['picurl'] ? $ftpWeb.$info['picurl'] : '';
		//通用解析

		return $info;
	}

}